package com.eliauk.demo.controller.write;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.eliauk.demo.entity.wirte.ComplexHeadData;
import com.eliauk.demo.entity.wirte.DemoData;
import com.eliauk.demo.entity.wirte.IndexData;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("write5")
public class write5Controller {

    List<DemoData> data1 = ListData.demoData();
    List<IndexData> data2 = ListData.indexData();
    List<ComplexHeadData> data3 = ListData.complexHeadData();


    
    //这样的写法是不对的，EasyExcel框架设计时就是每次写入或读出时就把流关闭，
    // 下面的写法只会运行到.doWrite(data1); 导出的数据也只会包含data1
//    @GetMapping("download")
//    public String exportData(HttpServletResponse response) throws IOException {
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//        response.setCharacterEncoding("utf-8");
//        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
//        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
//        ServletOutputStream out = response.getOutputStream();
//
//        EasyExcel.write(out, DemoData.class).sheet(1).doWrite(data1);
//        EasyExcel.write(out, IndexData.class).sheet(2).doWrite(data2);
//        EasyExcel.write(out, ComplexHeadData.class).sheet(3).doWrite(data3);
//        return "success";
//    }


    /**
     * 这里演示同一张excel文件中，不同sheet导出不同对象数据
     */
    @GetMapping("download")
    public String exportData(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        ServletOutputStream out = response.getOutputStream();

        try (ExcelWriter excelWriter = EasyExcel.write(out).build()) {
            for (int i = 1; i <= 3; i++) {
                // 每次创建一个新的sheet，使用不同的数据类和数据集
                WriteSheet sheet = EasyExcel.writerSheet(i, "Sheet" + i).head(getDataClass(i)).build();
                List<?> data = getData(i);
                excelWriter.write(data, sheet);
            }
        }
        return "success";
    }

    private Class<?> getDataClass(int i) {
        // 返回不同的数据类，根据不同的sheet
        if (i == 1) {
            return DemoData.class;
        } else if (i == 2) {
            return IndexData.class;
        } else {
            return ComplexHeadData.class;
        }
    }

    private List<?> getData(int i) {
        // 返回不同的数据集，根据不同的sheet
        if (i == 1) {
            return data1;
        } else if (i == 2) {
            return data2;
        } else {
            return data3;
        }
    }
}
